package com.elitel.frame.core.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class ReadXml {

    /**
     * dmo4j读取xml
     * @param xmlPath
     * @return
     */
    public Map<String, String> getXmlDom4j(String xmlPath) {
        String path = getWebInfPath(xmlPath);
        Map<String, String> map = new HashMap<String, String>();
        SAXReader reader = new SAXReader();
        InputStream in = null;
        try {
            in = new FileInputStream(path);
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
        InputStreamReader isr = new InputStreamReader(in);// 读取
        try {
            Document document = reader.read(isr);
            Element root = document.getRootElement();
            for (Iterator it = root.elementIterator(); it.hasNext();) {
                Element element = (Element) it.next();
                map.put(element.attributeValue("name"), element.getText());
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 读取properties
     * @param xmlPath
     * @return
     */
    public Map<String,String> getXmlProperties(String xmlPath) {
        String path =getWebInfPath(xmlPath);
        Map<String, String>  map= new HashMap<String, String>();
        try {
           // 读取配置文件
            Properties properties = new Properties();
           properties.loadFromXML(new FileInputStream(path));
           
           Enumeration<Object> set2 = properties.keys();// 返回键的枚举  
           
           while (set2.hasMoreElements()) {  
               String key = (String) set2.nextElement();  
             map.put(key, properties.getProperty(key));
        
     }
         } catch (Exception e) {
            e.printStackTrace();
            }
        return map;
    }
    
    /**
     * 获取项目路径
     * @param file
     * @return
     */
    public  String getWebInfPath(String file) {
        URL url = getClass().getProtectionDomain().getCodeSource()
                .getLocation();
        String path = url.toString();
        int index = path.indexOf("WEB-INF");

        if (index == -1) {
            index = path.indexOf("classes");
        }

        if (index == -1) {
            index = path.indexOf("bin");
        }

        path = path.substring(0, index);

        if (path.startsWith("zip")) {// 当class文件在war中时，此时返回zip:D:/...这样的路径
            path = path.substring(4);
        } else if (path.startsWith("file")) {// 当class文件在class文件中时，此时返回file:/D:/...这样的路径
            path = path.substring(6);
        } else if (path.startsWith("jar")) {// 当class文件在jar文件里面时，此时返回jar:file:/D:/...这样的路径
            path = path.substring(10);
        }
        try {
            path = URLDecoder.decode(path, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return path + "WEB-INF/classes/" + file;
    }

}
